//
//  DiaryVC.m
//  NewNest
//
//  Created by hesiyao on 16/1/24.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "DiaryVC.h"
#import "DiaryCell.h"
#import "DiaryModal.h"
#import <AFNetworking.h>
#import <AFHTTPSessionManager.h>
#import <UIImageView+WebCache.h>
#import "DiaryDetailPageVC.h"
@interface DiaryVC () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableview;
@property (nonatomic, retain) NSMutableArray *dataArr;
@end

@implementation DiaryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //navigation
    self.navigationItem.title = @"装修日记";
    
    // 处理数据
    [self dataHandler];
    
    // 创建视图
    [self createSubviews];
    
    // 隐藏导航栏
    [self hidden];
}

/// 处理数据
- (void)dataHandler
{
    NSString *urlStr = @"http://api.wsq.umeng.com/v2/topic/feeds?os=iOS&openudid=ab2665bf1689f98c2225a0fdbd3c01b8ed54b61c&sdkv=2.2.1&ak=5636cff167e58ef91b0001ae&de=iPhone5,4&mac=02:00:00:00:00:00&uid=56a330927019c97215dad1ed&topic_id=568f4e0c7019c9432c13e3c6&count=20";
    [[AFHTTPSessionManager manager] GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"装修日记解析成功");
        self.dataArr = [DiaryModal modelHanderWithArray:responseObject[@"items"]];
        
        [_tableview reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"装修日记解析失败");
    }];
}

/// 创建视图
- (void)createSubviews
{
    self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [_tableview registerClass:[DiaryCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableview];
    [_tableview release];
}

// 设置cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

//设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiaryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    DiaryModal *m = _dataArr[indexPath.row];
    cell.userNameLabel.text = m.name;
    cell.contentLabel.text = m.content;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.backImgUrl]];
    [cell.userImgview sd_setImageWithURL:[NSURL URLWithString:m.imgUrl]];
    return cell;
}

// 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 2;
}

/// 点击进入详情页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiaryDetailPageVC *detailVC = [[DiaryDetailPageVC alloc] init];
    detailVC.modal = _dataArr[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}

/// 脚部
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"没有更多咯~";
}

- (void)hidden
{
    // 导航栏隐藏设置
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        // 取消ScrollView的内容自动调整
        self.automaticallyAdjustsScrollViewInsets = NO;
        //tableView边界预留
        UIEdgeInsets insets = self.tableview.contentInset;
        // 让上边界的预留值和导航栏相等
        insets.top = self.navigationController.navigationBar.bounds.size.height;
        //把修改后的边界预留值赋给tableView
        self.tableview.contentInset = insets;
        // 滚动指标点到滚到视图的边缘的距离
        self.tableview.scrollIndicatorInsets = insets;
    }
}

// 导航栏滚动
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if (velocity.y > 0) {
        self.navigationController.navigationBar.hidden = YES;
        self.tabBarController.tabBar.hidden = YES;
    }else{
        self.navigationController.navigationBar.hidden = NO;
        self.tabBarController.tabBar.hidden = NO;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
